package com.frame.detection.client;

import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Component
public class ImageDetectClientImpl implements ImageDetectClient {

    /**
     * get detect image post
     *
     * @param paramMap paramMap
     * @return
     */
    @Override
    public String getDetectImagePost(MultiValueMap<String, String> paramMap) {

        //通过8081端口向python端发送请求
        // step 1. request
//        String url = "http://127.0.0.1:8081/detect/imageDetect";
        String url = "http://127.0.0.1:8081/detect/imageDetect";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");
        return response.getBody();
    }


    @Override
    public String getDetectImagePost_h(MultiValueMap<String, String> paramMap) {

        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_h";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_h";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_h";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_h";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }

    @Override
    public String getDetectImagePost_A(MultiValueMap<String, String> paramMap) {
        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_A";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_A";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_A";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_A";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }

    @Override
    public String getDetectImagePost_G(MultiValueMap<String, String> paramMap) {
        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_G";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_G";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_G";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_G";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }

    @Override
    public String getDetectImagePost_F(MultiValueMap<String, String> paramMap) {
        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_F";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_F";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_F";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_F";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }

    @Override
    public String getDetectImagePost_psnr(MultiValueMap<String, String> paramMap) {
        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_psnr";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_psnr";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_psnr";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_psnr";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }

    @Override
    public String getDetectImagePost_ssim(MultiValueMap<String, String> paramMap) {
        //        073机
//        String url = "http://region-3.autodl.com:35463/detect/imageDetect_ssim";
        //        083机
//        String url = "http://region-3.autodl.com:49464/detect/imageDetect_ssim";
        //        044机
//        String url = "http://region-3.autodl.com:46101/detect/imageDetect_ssim";
        //        097机
        String url = "http://region-3.autodl.com:45381/detect/imageDetect_ssim";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request;
        request = new HttpEntity<>(paramMap, headers);

        //step 2. post http call
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        Assert.isTrue(HttpStatus.OK == response.getStatusCode(), "http call is failed");

        return response.getBody();
    }
}